package org.deadline.model.config;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.deadline.model.baseapp.BaseEntity;
import org.deadline.model.user.Person;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

@Entity
public class Country extends BaseEntity  implements Serializable {

    @Size(min = 3, max = 100)
    private String countryCode;

    @NotNull
    @Size(min = 3, max = 100)
    private String countryName;

    @Size(min = 3, max = 100)
    private String countryCapital;

    @ManyToOne
    private Person createdBy;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country")
    private Set<StateProvince> stateProvince = new HashSet<StateProvince>();
    
    @Id
	@Column(name="id",unique=true,nullable=false)
	@SequenceGenerator(name="country_seq_gen",sequenceName="country_seq")
	@GeneratedValue(generator="country_seq_gen",strategy=GenerationType.TABLE)
	@Generated(GenerationTime.INSERT)
	private Long id;


	public String getCountryCode() {
        return this.countryCode;
    }

	public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

	public String getCountryName() {
        return this.countryName;
    }

	public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

	public String getCountryCapital() {
        return this.countryCapital;
    }

	public void setCountryCapital(String countryCapital) {
        this.countryCapital = countryCapital;
    }

	public Person getCreatedBy() {
        return this.createdBy;
    }

	public void setCreatedBy(Person createdBy) {
        this.createdBy = createdBy;
    }

	public Set<StateProvince> getStateProvince() {
        return this.stateProvince;
    }

	public void setStateProvince(Set<StateProvince> stateProvince) {
        this.stateProvince = stateProvince;
    }

	public String toJson() {
        return new JSONSerializer().exclude("*.class").deepSerialize(this);
    }

	public static Country fromJsonToCountry(String json) {
        return new JSONDeserializer<Country>().use(null, Country.class).deserialize(json);
    }

	public static String toJsonArray(Collection<Country> collection) {
        return new JSONSerializer().exclude("*.class").deepSerialize(collection);
    }

	public static Collection<Country> fromJsonArrayToCountrys(String json) {
        return new JSONDeserializer<List<Country>>().use(null, ArrayList.class).use("values", Country.class).deserialize(json);
    }
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	private static final long serialVersionUID = 1L;

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
